package io.gitee.bluemiaomiao.dataanalyzebackend.config;

import io.gitee.bluemiaomiao.dataanalyzebackend.domian.Ruser;
import io.gitee.bluemiaomiao.dataanalyzebackend.repos.RuserDao;
import io.gitee.bluemiaomiao.dataanalyzebackend.repos.RuserRoleDao;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: TLang @Date: 2020/11/8
 *
 * @description:
 */
@Service
public class UserService implements UserDetailsService {

  @Resource private RuserDao ruserDao;

  @Resource private RuserRoleDao ruserRoleDao;

  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    System.out.println("user+++++" + username);
    Ruser ruser = this.ruserDao.queryByUsername(username);
    System.out.println(null == ruser);
    if (null == ruser) {
      throw new UsernameNotFoundException("账户不存在！");
    }
    System.out.println("数据库查询权限" + this.ruserRoleDao.querybyuid(ruser.getRole()));
    ruser.setRoles(this.ruserRoleDao.querybyuid(ruser.getRole()));
    return ruser;
  }
}
